package rsj.api.orgp;

import com.rlyy.aibase.db.Dbink;
import com.rlyy.aibase.utils.base.Box;
import com.rlyy.aibase.utils.base.MapNode;
import common.util.Assert;
import common.util.BoxUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import rsj.api.user.BaseLink;
import rsj.db.PostgresqlConn;
import rsj.service.OrgpService;
import rsj.sql.user.UserDao;

import java.sql.Connection;
import java.sql.SQLException;

/**
 * 查询自己的充值记录
 *
 * @author yuanqinglong
 * @since 2021/6/21 15:42
 */
@RestController
public class MyRechargeListController extends BaseLink {

    @Autowired
    private UserDao userDao;

    @Autowired
    private OrgpService orgpService;

    @Override
    @RequestMapping(value = "rsj_my_rechargeList")
    public Box executeLink(Box box) throws SQLException {
        MapNode user = box.getMapNode("$user");
        MapNode data = box.getMapNode("data");
        String userId = user.getValue("User_Id");
        Assert.isNotBlank(userId, "用户id不可为空");
        MapNode mapNode = userDao.queryOrgIdByUser(userId);
        String userOrgId = mapNode.getValue("User_Org_Id");
        data.putNode("Org_Id",userOrgId);
        Connection connection = PostgresqlConn.getConnection();
        MapNode rechargeList ;
        try {
            rechargeList = orgpService.rechargeList(connection, data);
        } catch (SQLException e) {
            logger.error("invoke--->" + box.getApi() + " error "
                    + ", arguments is " + box.toJson() , e);
            return BoxUtil.errorBox(box,  "服务器忙！" );
        } finally {
            Dbink.close(connection);
        }
        return BoxUtil.successBox(box, "查询成功！").putMapNode("data", rechargeList);
    }
}
